//
//  HYsongsMenuController.m
//  TTLestening
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "HYsongsMenuController.h"
#import "HYcellForSongsMenu.h"
#import "HYModelForSongsMenu.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "NetworkingHandlerOfBlock.h"
#import <MJRefresh/MJRefresh.h>
#import "HYTypeForChooseControl.h"
#import "XCHotListController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface HYsongsMenuController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionForMenu;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayoutForMenu;
@property (nonatomic, strong) UIView *viewForHeader;
@property (nonatomic, strong) NSMutableArray *arrForSongsMenu;
@property (nonatomic, strong) NSString *str;
@property (nonatomic, assign) NSInteger pageRefresh;
@property (nonatomic, strong) UIButton *buttonForType;

@end

@implementation HYsongsMenuController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.arrForSongsMenu = [NSMutableArray array];
    
    self.pageRefresh = 1;
    
    self.str = @"http://search.dongting.com/songlist/search?q=tag:%E6%9C%80%E7%83%AD&page=1&size=10&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPad4%2C4&f=f320&s=s330&imsi=&hid=&splus=9.2&active=1&net=2&openudid=2928af1fecbb94c32045ea1742d7f5be6defe28e&idfa=87EE45D3-F1BC-49C6-AD99-E17773876586&utdid=Vu9EaFGgHV4DAPqTc15UrKj8&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude=";
    
    [self createNavigation];
    
    self.viewForHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 6)];
    
    self.viewForHeader.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.viewForHeader];
    
    self.buttonForType = [[UIButton alloc] initWithFrame:CGRectMake(20, self.viewForHeader.frame.size.height / 1.6, self.viewForHeader.frame.size.width / 5, self.viewForHeader.frame.size.height / 3)];
    
    [self.viewForHeader addSubview:self.buttonForType];
    
    [self.buttonForType setTitle:@"全部  ❯" forState:UIControlStateNormal];
    
    self.buttonForType.layer.borderWidth = 2;
    
    self.buttonForType.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    [self.buttonForType setTitleColor:[UIColor grayColor]forState:UIControlStateNormal];
    
    [self.buttonForType addTarget:self action:@selector(TypeAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self createCollection];
    
    [self handleData];
    
    [self handleRefresh];
}

- (void)TypeAction:(UIButton *)button {
    
    HYTypeForChooseControl *typeWithChoose = [[HYTypeForChooseControl alloc] init];
    
    [self.navigationController pushViewController:typeWithChoose animated:YES];
}

#pragma mark  创建NavigationController
- (void)createNavigation {
    
    self.navigationItem.title = @"歌单广场";
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName : [UIFont fontWithName:@"AmericanTypewriter-Bold" size:22],}];
}


- (void)createCollection {
    
    [self createFlowLayout];
    
    self.collectionForMenu = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT / 6, WIDTH, HEIGHT - HEIGHT / 6 - 49) collectionViewLayout:self.flowLayoutForMenu];
    
    self.collectionForMenu.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.collectionForMenu];
    
    self.collectionForMenu.dataSource = self;
    self.collectionForMenu.delegate = self;
    
    [self.collectionForMenu registerClass:[HYcellForSongsMenu class] forCellWithReuseIdentifier:@"songsMenu"];
    
}


- (void)createFlowLayout {
    
    self.flowLayoutForMenu = [[UICollectionViewFlowLayout alloc] init];
    
    self.flowLayoutForMenu.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2);
    
    self.flowLayoutForMenu.minimumInteritemSpacing = 10;
    self.flowLayoutForMenu.minimumLineSpacing = 50;
    
    self.flowLayoutForMenu.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

}


- (void)handleData {
    
    [NetworkingHandler networkingWithUrl:self.str completeHandle:^(NSURLResponse *response, NSData *data, id result) {
       
        NSArray *array = [result objectForKey:@"data"];
        
        for (NSDictionary *dic in array) {
            
            HYModelForSongsMenu *model = [[HYModelForSongsMenu alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrForSongsMenu addObject:model];
        }
        
        [self.collectionForMenu reloadData];
        
        [self.collectionForMenu.mj_footer endRefreshing];

    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.arrForSongsMenu.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HYcellForSongsMenu *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"songsMenu" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor grayColor];
    
    HYModelForSongsMenu *model = [self.arrForSongsMenu objectAtIndex:indexPath.item];
    
    [cell passMoldel:model];
    
    return cell;
    
}


- (void)handleRefresh {
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        NSString *str1 = @"http://search.dongting.com/songlist/search?q=tag:";
        NSString *str2 = @"%E6%9C%80%E7%83%AD&";

//        NSString *str3 = @"&size=10&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPad4%2C4&f=f320&s=s330&imsi=&hid=&splus=9.2&active=1&net=2&openudid=2928af1fecbb94c32045ea1742d7f5be6defe28e&idfa=87EE45D3-F1BC-49C6-AD99-E17773876586&utdid=Vu9EaFGgHV4DAPqTc15UrKj8&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude=";
   
//        NSString *str4 = [@"page=%ld", ++self.pageRefresh];
        
        self.str = [NSString stringWithFormat:@"%@%@page=%ld&size=10&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPad4%%2C4&f=f320&s=s330&imsi=&hid=&splus=9.2&active=1&net=2&openudid=2928af1fecbb94c32045ea1742d7f5be6defe28e&idfa=87EE45D3-F1BC-49C6-AD99-E17773876586&utdid=Vu9EaFGgHV4DAPqTc15UrKj8&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude=",str1, str2, ++self.pageRefresh];
        
        NSLog(@"%@",self.str);
//        self.str = [NSString stringWithFormat:@"http://search.dongting.com/songlist/search?q=tag:%E6%9C%80%E7%83%AD&page=%ld&size=10&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPad4%2C4&f=f320&s=s330&imsi=&hid=&splus=9.2&active=1&net=2&openudid=2928af1fecbb94c32045ea1742d7f5be6defe28e&idfa=87EE45D3-F1BC-49C6-AD99-E17773876586&utdid=Vu9EaFGgHV4DAPqTc15UrKj8&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude=", ++self.pageRefresh];
        

        self.str = [NSString stringWithFormat:@"%@%@page=%ld&size=10&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPad4%%2C4&f=f320&s=s330&imsi=&hid=&splus=9.2&active=1&net=2&openudid=2928af1fecbb94c32045ea1742d7f5be6defe28e&idfa=87EE45D3-F1BC-49C6-AD99-E17773876586&utdid=Vu9EaFGgHV4DAPqTc15UrKj8&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude=",str1, str2, ++self.pageRefresh];

        [self handleData];
        
    }];
    
    self.collectionForMenu.mj_footer = footer;
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
     XCHotListController *singerControl = [[XCHotListController alloc] init];
    HYModelForSongsMenu *model = [self.arrForSongsMenu objectAtIndex:indexPath.item];
    
    singerControl.value = model._id;
    
    [self.navigationController pushViewController:singerControl animated:YES];
    
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
